package com.wjf.shortlink.shortlink.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjf.shortlink.shortlink.dao.entity.ShortLinkDAO;
import com.wjf.shortlink.shortlink.dao.mapper.ShortLinkMapper;
import com.wjf.shortlink.shortlink.dto.req.RecycleBinRecoverReqDTO;
import com.wjf.shortlink.shortlink.dto.req.RecycleBinRemoveReqDTO;
import com.wjf.shortlink.shortlink.dto.req.RecycleBinSaveReqDTO;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkRecycleBinPageReqDTO;
import com.wjf.shortlink.shortlink.dto.resp.ShortLinkPageRespDTO;
import com.wjf.shortlink.shortlink.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.wjf.shortlink.shortlink.common.constant.RedisKeyConstant.GOTO_IS_NULL_SHORT_LINK_KEY;
import static com.wjf.shortlink.shortlink.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_KEY;

//回收站管理接口实现层
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLinkDAO> implements RecycleBinService {
    private final StringRedisTemplate stringRedisTemplate;

    //保存回收站
    @Override
    public void saveRecycleBin(RecycleBinSaveReqDTO recycleBinSaveReqDTO) {
        LambdaUpdateWrapper<ShortLinkDAO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDAO.class)
                .eq(ShortLinkDAO::getFullShortUrl, recycleBinSaveReqDTO.getFullShortUrl())
                .eq(ShortLinkDAO::getGid, recycleBinSaveReqDTO.getGid())
                .eq(ShortLinkDAO::getEnableStatus, 0)
                .eq(ShortLinkDAO::getDelFlag, 0);
        ShortLinkDAO shortLinkDAO = ShortLinkDAO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(shortLinkDAO, updateWrapper);
        stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY, recycleBinSaveReqDTO.getFullShortUrl()));
    }

    //分页查询回收站里短链接
    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLink(ShortLinkRecycleBinPageReqDTO shortLinkRecycleBinPageReqDTO) {
        IPage<ShortLinkDAO> resultPage = baseMapper.pageRecycleBinLink(shortLinkRecycleBinPageReqDTO);
        return resultPage.convert(each -> {
            ShortLinkPageRespDTO shortLinkPageRespDTO = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
            shortLinkPageRespDTO.setDomain("http://" + shortLinkPageRespDTO.getDomain());
            return shortLinkPageRespDTO;
        });
    }

    //从回收站恢复短链接
    @Override
    public void recoverRecycleBin(RecycleBinRecoverReqDTO recycleBinRecoverReqDTO) {
        LambdaUpdateWrapper<ShortLinkDAO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDAO.class)
                .eq(ShortLinkDAO::getFullShortUrl, recycleBinRecoverReqDTO.getFullShortUrl())
                .eq(ShortLinkDAO::getGid, recycleBinRecoverReqDTO.getGid())
                .eq(ShortLinkDAO::getEnableStatus, 1)
                .eq(ShortLinkDAO::getDelFlag, 0);
        ShortLinkDAO shortLinkDAO = ShortLinkDAO.builder()
                .enableStatus(0)
                .build();
        baseMapper.update(shortLinkDAO, updateWrapper);
        stringRedisTemplate.delete(String.format(GOTO_IS_NULL_SHORT_LINK_KEY, recycleBinRecoverReqDTO.getFullShortUrl()));
    }

    //从回收站移除短链接
    @Override
    public void removeRecycleBin(RecycleBinRemoveReqDTO recycleBinRemoveReqDTO) {
        LambdaUpdateWrapper<ShortLinkDAO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDAO.class)
                .eq(ShortLinkDAO::getGid, recycleBinRemoveReqDTO.getGid())
                .eq(ShortLinkDAO::getEnableStatus, 1)
                .eq(ShortLinkDAO::getDelFlag, 0)
                .eq(ShortLinkDAO::getFullShortUrl, recycleBinRemoveReqDTO.getFullShortUrl())
                .eq(ShortLinkDAO::getDelTime, 0L);
        ShortLinkDAO shortLinkDAO = ShortLinkDAO.builder()
                .delTime(System.currentTimeMillis())
                .build();
        shortLinkDAO.setDelFlag(1);
        baseMapper.update(shortLinkDAO, updateWrapper);
    }
}
